package com.ferrlo.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import weibo4j.Users;
import weibo4j.http.AccessToken;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;

import com.ferrlo.utils.RRAppConfig;
import com.qq.connect.QQConnectException;
import com.qq.connect.oauth.Oauth;
import com.renren.api.client.RenrenApiConfig;

@Controller
public class ExtLoginController {
	
	
	@RequestMapping(value="/qLogin")
	public String qLogin(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/html;charset=utf-8");

        try {
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (QQConnectException e) {
            e.printStackTrace();
        }
		return "index";
	}
	
	@RequestMapping(value="/qAfterLogin")
	public String qAfterLogin(){
		
		System.out.println("==== AFTER QQ Login ====");
		
		return "index";
	}
	
	
	@RequestMapping(value="/wLogin")
	public String WbLogin(HttpServletRequest request,HttpServletResponse response) throws WeiboException, IOException{
		weibo4j.Oauth oauth = new weibo4j.Oauth();
		response.sendRedirect(oauth.authorize("code"));
		return "index";
	}
	
	@RequestMapping(value="/wAfterLogin")
	public String wAfterLogin(HttpServletRequest request) throws WeiboException{
		String uid = "";
		weibo4j.Oauth oauth = new weibo4j.Oauth();

		System.out.println("==== AFTER Weibo Login ====");
		String code = request.getParameter("code");
		AccessToken accessToken = oauth.getAccessTokenByCode(code);
		String token = accessToken.getAccessToken();
		uid = accessToken.getUid();
		System.out.println("====TOKEN: "+accessToken);
		System.out.println("====TOKEN: "+token);
		
		System.out.println("===== !!! "+uid);
		Users users = new Users(token);
		User user = users.showUserById(uid);
		
		System.out.println("==== USERLLLL "+user.getScreenName());

		return "index";
	}

	@RequestMapping(value="/rLogin")
	public String rLogin(){
		
		RenrenApiConfig.renrenApiKey = RRAppConfig.API_KEY;
		RenrenApiConfig.renrenApiSecret = RRAppConfig.APP_SECRET;
		
		
		
		return "demo";
	}
	
	@RequestMapping(value="/rAfterLogin")
	public String rAfterLogin(){
		
		return "index";
	}
}
